import { ref } from 'vue'
import { showFailToast } from 'vant'
import { getProjectInfo, type ProjectInfo } from '@/api/project'
import { getHouseList } from '@/api/house'
import type { HouseInfo } from '@/mock/house'

export function useProjectData() {
  const projectInfo = ref<ProjectInfo>({
    id: '',
    title: '',
    subtitle: '',
    description: '',
    buildingList: [],
    totalCount: 0
  })

  const houseList = ref<HouseInfo[]>([])
  const loading = ref(false)

  // 获取楼盘信息
  const fetchProjectInfo = async (projectId: string) => {
    try {
      const data = await getProjectInfo(projectId)
      projectInfo.value = data
      return data
    } catch (error) {
      showFailToast('获取楼盘信息失败')
      console.error('获取楼盘信息失败:', error)
      throw error
    }
  }

  // 获取房源列表
  const fetchHouseList = async (params: { project_id: string; building_id: string }) => {
    try {
      loading.value = true
      const data = await getHouseList(params)
      houseList.value = data
      return data
    } catch (error) {
      showFailToast('获取房源列表失败')
      console.error('获取房源列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  return {
    projectInfo,
    houseList,
    loading,
    fetchProjectInfo,
    fetchHouseList
  }
}
